package com.hangzhou.xyj.ui.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.model.DynamicModel;
import com.hangzhou.xyj.model.DynamicPageInfoModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.GetCommunityFocusDynamicHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetCommunityFocusDynamicReq;
import com.hangzhou.xyj.ui.adapter.FollowDynamicsAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.fragment.base.BaseFragment;
import com.hangzhou.xyj.ui.widget.DividerItemDecoration;

import java.util.List;

/**
 * Created by neo.duan on 2015/12/23. 关注动态
 */
public class FollowDynamicsFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    private FollowDynamicsAdapter mAdapter;
    private int mUserId;//用户ID
    private int mPageSize = 10;//每页条目数

    @Override
    public void initTop() {
        enableTop(false);
        enableRefresh(true);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.layout_common_recycleview);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_common);
        // 设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,
                LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置水平分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
                , R.drawable.line_divider));
        mRecyclerView.setAdapter(mAdapter = new FollowDynamicsAdapter(mRecyclerView, null));
    }

    @Override
    public void initDatas() {
        if (XYJApplication.getUserInfo() == null) {
            return;
        }
        mUserId = Integer.valueOf(CommonPreferences.getUserId(mContext));
        mAdapter.setmPageSize(mPageSize);
        getData(1);
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int currentPage = mAdapter.getCurrentPage(mPageSize);
                        getData(currentPage);
                    }
                }, 500);
            }
        });
    }

    public void getData(final int currentPage) {
        GetCommunityFocusDynamicReq getCommunityFocusDynamicReq = new GetCommunityFocusDynamicReq(mUserId, currentPage, mPageSize);
        HttpLoader.getInstance().sendRequest(new GetCommunityFocusDynamicHandler(getCommunityFocusDynamicReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                hideParentLoadingDialog();
                List<DynamicModel> data = ((DynamicPageInfoModel) model).getDynamicList();
                mAdapter.setIntentTag(EIntentTagType.TAG_COMMUNITY_FOCUS_DYNAMIC.getValue());
                if (currentPage == 1 && (data == null || data.isEmpty())) {
                    setNoDataTip(true, "暂时没有数据哦~");
                } else if (currentPage == 1) {
                    setNoDataTip(false, null);
                    mAdapter.load(data);
                } else {
                    setNoDataTip(false, null);
                    mAdapter.update(data);
                }
            }

            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

        }, 0));
    }
}
